package cn.pn.project.mapper.user;

import cn.pn.project.pojo.user.Attention;
import org.apache.ibatis.annotations.*;

import java.sql.SQLException;
import java.util.List;

@Mapper
public interface AttentionDao {

    /**
     * 添加关注
     * @param attention 不能有id
     * @return
     * @throws SQLException
     */
    @Insert("INSERT INTO `pn_user`.`attention` (  `attentionId`,  `userId`,  `commodityId`,    `more1`,  `more2`,  `more3`) VALUES  (   #{att.attentionId},   #{att.userId},   #{att.commodityId},    #{att.more1},   #{att.more2},   #{att.more3}  );")
    Integer insertAttention(@Param("att")Attention attention) throws SQLException;

    /**
     * 删除关注
     * @param attentionId
     * @return
     * @throws SQLException
     */
    @Delete("DELETE FROM `pn_user`.`attention` WHERE attentionId=#{attentionId} ;")
    Integer delAttention(@Param("attentionId") Integer attentionId) throws SQLException;

    /**
     * 查询已经关注的东东~~
     * @param attention
     * @return
     * @throws SQLException
     */
    @Select("<script>" +
            "SELECT\n" +
                "`attentionId`,\n" +
                "`userId`,\n" +
                "`commodityId`,\n" +
                "`more1`,\n" +
                "`more2`,\n" +
                "`more3`\n" +
            "FROM\n" +
                "`pn_user`.`attention`a\n" +
            "<where>\n" +
                "<if test='attention.userId!=0 and attention.userId!=null'>\n" +
                    "AND a.userId=#{attention.userId}\n" +
                "</if>\n" +
            "</where>" +
            "</script>")
    List<Attention> getAllAttention(@Param("attention") Attention attention) throws SQLException;

    /**
     * 查询出指定用户的指定关注商品关系数据
     * @param attention
     * @return
     * @throws SQLException
     */
    @Select("<script>" +
            "SELECT\n" +
            "  `attentionId`,\n" +
            "  `userId`,\n" +
            "  `commodityId`,\n" +
            "  `more1`,\n" +
            "  `more2`,\n" +
            "  `more3`\n" +
            "FROM\n" +
            "  `pn_user`.`attention` a" +
            "<where>\n" +
                "<if test='attention.userId!=0 and attention.userId!=null'>\n" +
                    "AND a.userId=#{attention.userId}\n" +
                "</if>\n" +
                "<if test='attention.commodityId!=null and attention.commodityId!=0  '>\n" +
                    "AND a.commodityId=#{attention.commodityId}\n" +
                "</if>\n" +
            "</where>" +
            "</script>")
    Attention findAttention(@Param("attention")Attention attention)throws SQLException;

}
